import mx.events.EventDispatcher;
import mx.utils.Delegate;
import com.live5.liveplayer.Playlist;
import com.live5.liveplayer.Controller;
class com.live5.liveplayer.MediaPlayer extends MovieClip
{
	private var autoLoop:Boolean=false;
	private var autoPlay:Boolean=true;
	private var playlist:Playlist;
	private var isPaused=false;
	var MediaType:String;
	var Canvas:MovieClip;
	var MediaController:Controller;
	public function addEventListener(type:String,target:Function):Void{};
	public function removeEventListener(type:String,target:Function):Void{};
	public var dispatchEvent:Function;
	function MediaPlayer()
	{
	 EventDispatcher.initialize(this);
	 MediaController.NextBtn.onRelease=Delegate.create(this,PlayNext);
	 MediaController.PrevBtn.onRelease=Delegate.create(this,PlayPrev);
	 MediaController.PauseBtn.onRelease=Delegate.create(this,Pause);
	}
	
	function Play(pl:Playlist){
		if(arguments[0]==null){
			throw new Error("Playlist not specified!");
		}
		if(pl.Count==0){
			throw new Error("Playlist is empty.");
		}
		playlist = pl;
		if(playlist.Current==null){
		    playlist.MoveNext();
		}
		DoPlay();
	}
	
	/**
	* Override this method.
	*/
	private function DoPlay(){
		this.MediaController.PauseBtn.obj.gotoAndStop(2);
		trace("current media:"+playlist.Current.Type);
		if(playlist.Next==null){
			this.MediaController.NextBtn.enabled=false;
		}
		else{
			this.MediaController.NextBtn.enabled=true;
		}
		if(playlist.Prev==null){
			this.MediaController.PrevBtn.enabled=false;
		}
		else{
			this.MediaController.PrevBtn.enabled=true;
		}
	}
	
	/**
	* Override this method.
	*/
	private function Pause(){
		
		isPaused=!isPaused;
		if(this.isPaused){
			this.MediaController.PauseBtn.obj.gotoAndStop(1);
		}else{
			this.MediaController.PauseBtn.obj.gotoAndStop(2);
		}
	}
	
	/**
	* Override this method.
	*/
	public function Stop(){
		
	}
	function PlayNext(){
		trace("play next");
		Stop();
		if(playlist.Next!=null){
			playlist.MoveNext();
			if(playlist.Current.Type==this.MediaType){
			    DoPlay();
			}
			else{
				this.OnSwitchPlayer();
			}
		}else{
			this.OnPlaylistComplete();
		}
	}
	function PlayPrev(){
		Stop();
		if(playlist.Prev!=null){
			playlist.MovePrev();
			if(playlist.Current.Type==this.MediaType){
			    DoPlay();
			}
			else{
				this.OnSwitchPlayer();
			}
		}else{
			throw new Error("No previous media.");
		}
	}
	[Event("complete")]
	function OnComplete():Void{
		if(autoPlay){
			this.PlayNext();
		}
		var eventObj = new Object();
        eventObj.type = "complete";
        eventObj.target = this;
		this.dispatchEvent(eventObj);
	}
	[Event("playlistComplete")]
	function OnPlaylistComplete():Void{
		var eventObj = new Object();
        eventObj.type = "playlistComplete";
        eventObj.target = this;
		this.dispatchEvent(eventObj);
	}
	[Event("switchPlayer")]
	function OnSwitchPlayer():Void{
		var eventObj = new Object();
        eventObj.type = "switchPlayer";
        eventObj.target = this;
		this.dispatchEvent(eventObj);
	}
}
